﻿#include "stdafx.h"
#include "Src/FUI_InputEvent.h"
#include "Src/FUI_FuncInterface.h"

createDeviceApp::createDeviceApp()
:szImei("123456789012345")
{

}

bool createDeviceApp::Create()
{
	FUI_INIT it;
	it.hwnd  = fge->GetHwnd();
	it.wnd_w = fge->GetWidthF();
	it.wnd_h = fge->GetHeightF();
	it.base_dir = FAppGetAssetsRootA();
	it.userPTR  = (DWORD_PTR)fge;
	FUI_InitFuncInterface(it);
	uiSys.Initialize(_FTA("FrameMain.fud"), &it);
	myWnd.LoadUI(&uiSys);
	
//	insA.id = 1;
//	insB.id = 2;
//	insA.ins = FFString(INS_A);
//	insB.ins = FFString(INS_B);
	
	GetMainInsText();

#ifdef _CTP_API
	
#ifdef WIN32
	
	//windows 下的处理
	
	pMDApi = CThostFtdcMdApi::CreateFtdcMdApi("", false, false);
	protocol.SetEventHandle(this);
	pMDApi->RegisterSpi(&protocol);
	pMDApi->RegisterFront(CTP_FRONT_ADDR);
	pMDApi->Init();
	
#else

	FAutoDll infoDll;
	CTP_CREATE_0 ctp_create;
	FFString os_file(CTP_QUOTE_SO);
	infoDll.Load(os_file);
	infoDll.GetFunc(ctp_create, _CreateFtdcMdApi_4);
	if (!ctp_create)
	{
		FOutDebugStringA(FAssertFmtA(_FTA("YCTP>> %s is null)\n"), "ctp_create"));
		return true;
	}

	FStringA conPath = FFAppGetTemp(_FFT(""));
	FOutDebugStringA(FAssertFmtA(_FTA("[err]YCTP>> %s >>> load Dialog.con)\n"), conPath.c_str()));
	//Create
	pMDApi = (*ctp_create)(conPath.c_str(), false, false);
	if (!pMDApi)
	{		    
		FOutDebugStringA(FAssertFmtA(_FTA("[err]YCTP>> %s ,err)\n"), "ctp_create"));
		return true;
	}
	else
	{		    
		FOutDebugStringA(FAssertFmtA(_FTA("[detail]YCTP>> %s ,ok)\n"), "ctp_create"));
	}
	//Connect
	protocol.SetEventHandle(this);
	pMDApi->RegisterSpi(&protocol);
	pMDApi->RegisterFront(CTP_FRONT_ADDR);
	pMDApi->Init();	
#endif // _WIN32

#endif

	return true;
}
void createDeviceApp::Destroy()
{
	myWnd.UnLoad();
	uiSys.Release();	
}
void createDeviceApp::Render()
{
    // 对渲染逻辑进行性能统计，性能日志存在log\[date-time]\profile - [app_name].log中
	FPF_FuncAuto;	    
	uiSys.Render();
	uiSys.RenderTips();
}
bool createDeviceApp::OnInput(FINPUT_PACKAGE& ipk)
{
	static bool is_down = false;
	switch (ipk.nType)
	{
	case eIT_KEYUP:
		if (ipk.p1 == VK_ESCAPE)
			fge->Quit();
		else if (ipk.p1 == 'A')
			fge->GetFont()->__OutputFntTex(eCD_DefFontID, _FTA("d:\\aaa.png"));
		break;
	case eIT_LBUTTON_DOWN:
		sprx = ipk.p2.v.v2.x;
		spry = ipk.p2.v.v2.y;
		is_down = true;
		break;
	case eIT_MOUSEMOVE:
		if (is_down)
		{
			sprx = ipk.p2.v.v2.x;
			spry = ipk.p2.v.v2.y;
		}
		break;
	case eIT_LBUTTON_UP:
		is_down = false;
		sprx = ipk.p2.v.v2.x;
		spry = ipk.p2.v.v2.y;
		break;
	}
	return FInputTransmitToUI(ipk, fge, &uiSys);
	return false;
}
	
void createDeviceApp::Update()
{
	uiSys.Update();
}
